﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;

public partial class Orders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            List<orders> allOrders = new List<orders>();
            foreach (var dept in service.getAllDepartments())
            {
                service.setFocusOnDepartment(dept.id);
                service.getDepartmentOrders();
                allOrders.AddRange(service.getSavedOrders().ToList());
            }

            gvOrders.DataSource = allOrders;
            gvOrders.DataBind();
            ScriptManager.RegisterStartupScript(this, typeof(Page), "jscript", "if($('#MainContent_lblMessage').text() != ''){$.blockUI({ message: '<p style=\"color:red;font-weight:bolder;padding:10px;font-size:larger;\">Completed or unavailable status cannot be changed!</p>',fadeIn: 500, timeout: 2000 })};", true);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrders_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvOrderedProducts.DataSource = service.getOrderedProducts(int.Parse(gvOrders.SelectedRow.Cells[0].Text));
            gvOrderedProducts.DataBind();

            lblMessage.Text = "";
            lblMessage.Visible = false;
            lblHelp.Visible = false;
            SelectedIndex.Text = gvOrders.SelectedRow.RowIndex.ToString();
            SelectedPage.Text = gvOrders.PageIndex.ToString();
            SelectedOrderID.Text = gvOrders.SelectedRow.Cells[0].Text;
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrderedProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            Label lblProductStatus = (Label)gvOrderedProducts.Rows[e.NewEditIndex].FindControl("lblProductStatus");
            if (lblProductStatus.Text.Equals("COMPLETED"))// || (lblProductStatus.Text.Equals("UNAVAILABLE")))
            {
                e.Cancel = true;
                lblMessage.Text = "Completed or unavailable status cannot be changed!";
                lblMessage.Visible = true;
                return;
            }
            else
            {
                lblMessage.Text = "";
                lblMessage.Visible = false;
            }
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvOrderedProducts.EditIndex = e.NewEditIndex;
            gvOrderedProducts.DataSource = service.getOrderedProducts(int.Parse(gvOrders.SelectedRow.Cells[0].Text));
            gvOrderedProducts.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrderedProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvOrderedProducts.EditIndex = -1;
            gvOrderedProducts.DataSource = service.getOrderedProducts(int.Parse(gvOrders.SelectedRow.Cells[0].Text));
            gvOrderedProducts.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrderedProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlProductStatus;

                ddlProductStatus = e.Row.FindControl("ddlProductStatus") as DropDownList;

                if (ddlProductStatus != null)
                {
                    List<string> statuses = service.getAllOrderStatuses().ToList();
                    ddlProductStatus.DataSource = statuses;
                    ddlProductStatus.SelectedIndex = statuses.IndexOf((e.Row.DataItem as orderedproducts).orderstatus);
                    ddlProductStatus.DataBind();
                }
            }
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrderedProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            DropDownList ddlProductStatus = (DropDownList)gvOrderedProducts.Rows[e.RowIndex].FindControl("ddlProductStatus");
            Label lblOrderID = (Label)gvOrderedProducts.Rows[e.RowIndex].FindControl("lblOrderID");
            Label lblProductCode = (Label)gvOrderedProducts.Rows[e.RowIndex].FindControl("lblProductCode");

            service.changeOrderedProductStatus(int.Parse(lblOrderID.Text), lblProductCode.Text, ddlProductStatus.SelectedValue);

            gvOrderedProducts.EditIndex = -1;
            gvOrderedProducts.DataSource = service.getOrderedProducts(int.Parse(gvOrders.SelectedRow.Cells[0].Text));
            gvOrderedProducts.DataBind();
            service.sendOrderUpdateToDepartment(lblProductCode.Text);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrderedProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            gvOrderedProducts.DataSource = service.getOrderedProducts(int.Parse(SelectedOrderID.Text));
            gvOrderedProducts.PageIndex = e.NewPageIndex;
            gvOrderedProducts.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            List<orders> allOrders = new List<orders>();
            foreach (var dept in service.getAllDepartments())
            {
                service.setFocusOnDepartment(dept.id);
                service.getDepartmentOrders();
                allOrders.AddRange(service.getSavedOrders().ToList());
            }

            gvOrders.DataSource = allOrders;
            gvOrders.PageIndex = e.NewPageIndex;
            gvOrders.DataBind();
            if (String.IsNullOrEmpty(SelectedIndex.Text))
            {
                gvOrders.SelectedIndex = -1;
            }
            else if (gvOrders.PageIndex.ToString().Equals(SelectedPage.Text))
            {
                int selection = -1;
                if (int.TryParse(SelectedIndex.Text, out selection))
                {
                    gvOrders.SelectedIndex = selection;
                }
                else
                {
                    gvOrders.SelectedIndex = -1;
                }
            }
            else
            {
                gvOrders.SelectedIndex = -1;
            }
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }
}